package ift604.projet.music;

import ift604.projet.feeling.Feeling;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

public class SQLPlaylistRepository extends SQLiteOpenHelper {

        // nom de la base de donnees
        static final String DB_NAME = "map_my_feelings.db";

        // no de version : utile pour les mises-a-jour,
        // e.g. si on change le schema de la BD
        static final int DB_VERSION = 2;

        // noms de la table et des colonnes de la BD
        public static final String TABLE_NAME = "songs_of_feelings";
        public static final String C_ID = BaseColumns._ID;
        public static final String C_FEELING = "feeling";
        public static final String C_SONG_TITLE = "titre";
        public static final String C_SONG_URL = "lien";

        public static SQLPlaylistRepository SQLPlaylistRepositoryWithContext(Context context){
            return new SQLPlaylistRepository(context);
        }
    
        private SQLPlaylistRepository(Context context) { //
            super(context, DB_NAME, null, DB_VERSION);
        }

        // Called only once, first time the DB is created
        @Override
        public void onCreate(SQLiteDatabase db) {

            // requete SQL pour la creation de la table
            String sql = "create table " + TABLE_NAME
                    + " (" + C_ID + " int primary key, "
                    + C_FEELING + " text, "
                    + C_SONG_TITLE + " text, "
                    + C_SONG_URL + " text)";

            // execution de la requete SQL
            db.execSQL(sql);

            Log.d(this.getClass().getName(), "onCreated sql: " + sql);
        }

        // Called whenever newVersion != oldVersion
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("drop table if exists " + TABLE_NAME);
            Log.d(this.getClass().getName(), "onUpdated");

            // run onCreate to get new database
            onCreate(db);
        }

        // lorsqu'on obtient une playlist a partir du site stereomood, on insere les chansons dans la BD locale
        // qui nous servira de cache si l'internet coupe eventuellement
        public void updatePlaylistOfFeeling(Playlist playlist, Feeling feeling) {
            SQLiteDatabase db = getWritableDatabase();

            db.delete(TABLE_NAME, C_FEELING +"='" + feeling.getName() +"'", null);

            ContentValues values = new ContentValues();
            for (Song song: playlist.songs()) {
                values.clear();
                values.put(C_FEELING, feeling.getName());
                values.put(C_SONG_TITLE, song.title());
                values.put(C_SONG_URL, song.fileURL());

                db.insert(TABLE_NAME, null, values);
            }

            db.close();
        }
        
        // pour obtenir une playlist a partir de ce qui avait ete sauvegarde
        public Playlist getCachePlaylist(Feeling feeling) {
    		SQLiteDatabase db = getReadableDatabase();

    		// Get the data from the database
    		String whereStmt = SQLPlaylistRepository.C_FEELING+"='"+feeling.getName()+"'";
    		
    		Cursor cursor =  db.query(SQLPlaylistRepository.TABLE_NAME, null, whereStmt, null, null, null, null);
    		Playlist playlist = Playlist.playListFromDB(cursor);
            cursor.close();

    		db.close();

    		return playlist;
        }
}
